/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2022-10-10 10:31:20
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2022-10-12 16:23:41
 * @FilePath: /vue3-datav/src/stores/user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia';
import {
    getToken,
    setToken,
    removeToken,
    getUserInfo as getLocaUserInfo,
    setUserInfo,
    removeUserInfo
} from '@/utils/auth';
import { localPubEncrypt } from '@/utils/encrypt';
import { login, logout, getUserInfo } from '@/api/login';
import defaultAvatar from '@/assets/images/profile.jpg';

export const useUserStore = defineStore('userStore', {
    state: (): { token: string, name: string, avatar: string, roles: string[], permissions: string[], userInfo: Record<string, unknown> | null } => {
        return {
            token: getToken() || '',
            name: '', // '铁山港管委'
            avatar: '', // '/static/images/profile.jpg'
            roles: [],
            permissions: [],
            userInfo: getLocaUserInfo()
        };
    },
    actions: {
        // 登录
        login(userInfo: {
            username: string;
            password: string;
            rememberMe: boolean;
            code: string;
            uuid: string;
        }) {
            const username = userInfo.username.trim();
            const password = localPubEncrypt(userInfo.password) || '';
            const { code } = userInfo;
            const { uuid } = userInfo;
            return login(username, password, code, uuid)
                .then((res) => {
                    setToken(res.data.token);
                    this.token = res.data.token;
                    return res;
                })
                .catch((error) => error);
        },
        // 获取用户信息
        getUserInfo() {
            return getUserInfo()
                .then((res) => {
                    const { user } = res.data;
                    const avatar =
                        user.avatar === '' ? defaultAvatar : user.avatar;
                    if (res.data.roles && res.data.roles.length > 0) {
                        this.roles = res.data.roles;
                        this.permissions = res.data.permissions;
                    } else {
                        this.permissions = ['ROLE_DEFAULT'];
                    }
                    const userInfo = {
                        ...user,
                        roles: res.data.roles,
                        permissions: res.data.permissions
                    };
                    this.name = user.nickName;
                    this.avatar = avatar;
                    this.userInfo = user.userInfo;
                    setUserInfo(userInfo);
                    return res;
                })
                .catch((error) => error);
        },
        // 退出系统
        logout() {
            return logout()
                .then(() => {
                    this.fedLogout();
                    return true;
                })
                .catch((error) => error);
        },
        // 前端 登出
        fedLogout() {
            this.userInfo = null;
            this.avatar = '';
            this.name = '';
            this.token = '';
            this.roles = [];
            this.permissions = [];
            removeToken();
            removeUserInfo();
            return Promise.resolve(true);
        }
    }
});
